package com.yanqu.road.entity.activity.sonhai.data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 兽群类
 */
public class SonHaiBeastGroupData {

    //兽群配置ID
    private int id;

    //异兽MAP
    private Map<Integer, SonHaiBeastData> beastDataMap = new LinkedHashMap<>();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Map<Integer, SonHaiBeastData> getBeastDataMap() {
        return beastDataMap;
    }

    public void setBeastDataMap(Map<Integer, SonHaiBeastData> beatsDataMap) {
        this.beastDataMap = beatsDataMap;
    }

    public SonHaiBeastGroupData(){}

    public SonHaiBeastGroupData(int id, Map<Integer, SonHaiBeastData> beastDataMap) {
        this.id = id;
        this.beastDataMap = beastDataMap;
    }

    /**
     * 获取总掉血
     * @return
     */
    public long getAllReBlood() {
        long allReBlood = 0;
        for (SonHaiBeastData beastData : this.beastDataMap.values()) {
            allReBlood += beastData.getAllReBlood();
        }
        return allReBlood;
    }

    /**
     * 获取总血量
     * @return
     */
    public long getAllBlood() {
        long allBlood = 0;
        for (SonHaiBeastData beastData : this.beastDataMap.values()) {
            allBlood += beastData.getAllBlood();
        }
        return allBlood;
    }

    /**
     * 获取当前血量
     * @return
     */
    public long getBlood() {
        long allBlood = this.getAllBlood();//总血量
        long allReBlood = this.getAllReBlood();//总掉血
        return Math.max(allBlood - allReBlood, 0);
    }

    /**
     * 判断是否被玩家打过
     * @param userId
     * @return
     */
    public boolean checkUserBeAttack(long userId) {
        for (SonHaiBeastData beastData : this.getBeastDataMap().values()) {
            if (beastData.getReBloodInfoMap().containsKey(userId)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 重置
     */
    public void reset() {
        for (SonHaiBeastData beastData : this.beastDataMap.values()) {
            beastData.setAllReBlood(0);
            beastData.setReBloodInfoMap(new ConcurrentHashMap<>());
        }
    }

    /**
     * 获取血量百分比
     * @return
     */
    public int getBloodRate() {
        long currentBlood = this.getBlood();
        long allBlood = this.getAllBlood();
        return BigDecimal.valueOf(currentBlood).divide(BigDecimal.valueOf(allBlood), 8, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100)).intValue();
    }
}
